package com.cuz.hot100;

import java.util.HashMap;

public class Q13RomaNumToInt {
    public static void main(String[] args) {
//        System.out.println(romaToInt1("MCMXCIV"));
        System.out.println(romaToInt1("LVIII"));
    }

    public static int romaToInt1(String str) {
        HashMap<String, Integer> help = new HashMap<>();
        help.put("M", 1000);
        help.put("D", 500);
        help.put("C", 100);
        help.put("L", 50);
        help.put("X", 10);
        help.put("V", 5);
        help.put("I", 1);
        char[] chars = str.toCharArray();
        int res = 0;
        for (int index = 0; index < chars.length; index++) {
            Integer pre = help.get(String.valueOf(chars[index]));
            if (index + 1 < chars.length) {
                Integer next = help.get(String.valueOf(chars[index + 1]));
                if (next != null && next > pre) {
                    res += next - pre;
                    index += 1;
                } else {
                    res += pre;
                }
            } else {
                res += pre;
            }
        }
        return res;
    }
}
